package com.maxd.controller.commodity;

import com.alibaba.fastjson.JSONArray;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.CommodityExquisite;
import com.maxd.service.Commodity.CommodityExquisiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 房
 * \* Date: 2020/5/20
 * \* Time: 14:32
 * \*
 * \
 */
@RestController
@Api(value = "精选商品", tags = {"精选商品"})
@RequestMapping(value = "/commodityExquisite")
public class CommodityExquisiteController {

    @Autowired
   private CommodityExquisiteService commodityExquisiteService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation("添加商品")
    @ResponseBody
    public Result save(CommodityExquisite commodityExquisite) {
        commodityExquisiteService.save(commodityExquisite);

        return ResultUtil.success();
    }

    @RequestMapping(value = "/saveList", method = RequestMethod.POST)
    @ApiOperation("添加商品多个商品")
    @ResponseBody
    public Result saveList(String commodityExquisites) {
        List<CommodityExquisite> list=JSONArray.parseArray(commodityExquisites,CommodityExquisite.class);
        commodityExquisiteService.saveAll(list);
        return ResultUtil.success();
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改商品")
    @ResponseBody
    public Result update(CommodityExquisite commodityExquisite) {
        commodityExquisiteService.update(commodityExquisite);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation("删除商品")
    @ResponseBody
    public Result deleteById(Long id) {
        commodityExquisiteService.deleteById(id);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    @ApiOperation("删除多个商品")
    @ResponseBody
    public Result deleteByIdList(String ids) {
        commodityExquisiteService.deleteByIdList(ids);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ApiOperation("根据id查看详细信息")
    @ResponseBody
    public Result selectById(Long id) {
        return ResultUtil.success(commodityExquisiteService.selectById(id));
    }

    @RequestMapping(value = "/selectByUserId", method = RequestMethod.GET)
    @ApiOperation("商品列表根据用户id查看")
    @ResponseBody
    public Result selectByUserId(Long userId) {
        return ResultUtil.success(commodityExquisiteService.selectByUserId(userId));
    }

    @RequestMapping(value = "/selectCommodityExquisite", method = RequestMethod.GET)
    @ApiOperation("查看所有信息")
    @ResponseBody
    public Result selectCommodityExquisite(int page,int size) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        Page<CommodityExquisite> commodityExquisites = commodityExquisiteService.selectCommodityExquisite(pageable);
        return ResultUtil.success(commodityExquisites);
    }



}